package com.lczyfz.istep.common.utils.domain;

import com.jcraft.jsch.*;
import com.lczyfz.istep.common.constants.SceneConstants;
import com.lczyfz.istep.common.utils.IdGen;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Created by chenqk on 2017/4/26.
 * http://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html
 */
public class Demo {

    public static void createSubDomain(String subDomain,String vhostHost, String vhostPort) {
        try {
            // nginx 所在服务器有权限的、可以登录的用户名


//            String username = "root";
//            // 对应用户的密码
//            String password = "cqk@simastudio";
//            // nginx 所在服务器
//            String host = "106.75.122.12";
//            // nginx 所在服务器的 ssh 端口
//            int port = 22;
//            // nginx vhost 配置文件目录
//            String nginxVhostConfPath = "/etc/nginx/conf.d";

//实验室服务器
//             nginx 所在服务器有权限的、可以登录的用户名
            String username = "root";
            // 对应用户的密码
            String password = "Lczy2016cfq";
            // nginx 所在服务器
            String host = "139.196.48.196";
//对方服务器
//            // nginx 所在服务器有权限的、可以登录的用户名
//            String username = "root";
//            // 对应用户的密码
//            String password = "NewDoone2017";
//            // nginx 所在服务器
//            String host = "202.101.116.71";

            // nginx 所在服务器的 ssh 端口
            int port = 2222;
            // nginx vhost 配置文件目录
//            String nginxVhostConfPath = "/etc/nginx/conf.d";
            String nginxVhostConfPath = "/alidata/server/nginx/conf/vhosts";

            JSch jSch = new JSch();
            Session session = jSch.getSession(SceneConstants.username, SceneConstants.host, SceneConstants.port);
            session.setPassword(SceneConstants.password);

            // 以下配置一般不需要修改
            Properties config = new Properties();
            // 当第一次连接服务器时，自动接受新的公钥
            config.put("StrictHostKeyChecking", "no");
            // compression
            config.put("compression.s2c", "zlib@openssh.com,zlib,none");
            config.put("compression.c2s", "zlib@openssh.com,zlib,none");
            config.put("compression_level", "9");

            session.setConfig(config);

            session.connect();

            //exec("ls", "UTF-8", session);
            //exec("mkdir dir_by_jsch", "UTF-8", session);

            // 域名的配置请根据实际项目调整
            NginxVhost vhost = new NginxVhost();
            vhost.setRootDomain(SceneConstants.rootDomain);
            vhost.setSubDomain(subDomain);
            List<UpstreamServer> upstreamServers = new ArrayList<UpstreamServer>();
            UpstreamServer upstreamServer = new UpstreamServer();
            upstreamServer.setHost(vhostHost);
            upstreamServer.setPort(Integer.parseInt(vhostPort));
            upstreamServers.add(upstreamServer);
            vhost.setUpstreamServers(upstreamServers);

            System.out.println("==="+vhost.getPlainConfig());

            ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();
            // go to nginx conf.d
            channelSftp.cd(nginxVhostConfPath);
            InputStream vhostInputStream = new ByteArrayInputStream(vhost.getPlainConfig().getBytes());
            channelSftp.put(vhostInputStream, vhost.getSubDomain() + ".conf");
            channelSftp.exit();
            vhostInputStream.close();
            channelSftp.disconnect();
            exec("(/alidata/server/nginx/sbin/nginx -s reload && echo \"success reload\")||echo \"failed reload\"", "UTF-8", session);
            session.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSchException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //rootDomain = "cubeliving.com.cn";//根域名
        String subDomain = IdGen.uuid();
        createSubDomain(subDomain,"139.196.48.191",SceneConstants.vhostPort);


    }

    public static void exec(String command, String charset, Session session) throws JSchException, IOException {
        ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
        channelExec.setCommand(command);
        channelExec.setInputStream(null);
        channelExec.setErrStream(System.err);


        channelExec.connect();
        InputStream in = channelExec.getInputStream();
        InputStreamReader r = new InputStreamReader(in, Charset.forName(charset));
        BufferedReader reader = new BufferedReader(r);

        String buf = null;
        while ((buf = reader.readLine()) != null) {
            System.out.println("Console Result :"+buf);
        }
        reader.close();

        channelExec.disconnect();


    }

    public static void close(Session session) {
        session.disconnect();
    }

}
